import { ObserverPath } from '../utils/pathEvent.js'
/**
 * reportingMethod
 * MonitorSdk.reportWay router|setTimeout|reload|none
 */
export function reportingMethod(MonitorSdk, _set_) {
  let _this = MonitorSdk
  let type = _this._CONFIG.reportWay
  switch (type) {
    case "router":
      ObserverPath(_this, _set_);
      break
    case "setTimeout":
      // eslint-disable-next-line no-case-declarations
      let timer = null
      clearInterval(timer)
      setInterval(() => {
        _set_.setTimeReport(_this._CONFIG.Timeout).then(res => {
          _this.sendReports(res)
        })
      }, _this._CONFIG.Timeout);
      break;
    case "reload":
      [onBeforeunload, onPagehide].forEach(fn => {
        fn(() => {
          _this.sendReports(_set_.reportData)
        })
      })    
      break;
    case "all":
      ObserverPath(_this, _set_);
      [onBeforeunload, onPagehide].forEach(fn => {
        fn(() => {
          _this.sendReports(_set_.reportData)
        })
      })   
      break;
    case "none":
      break;
    default:
      throw '请配置正确上报方式 router|setTimeout|reload';

  }
}
function onBeforeunload(callback) {
  window.addEventListener('beforeunload', callback, true)
}
function onPagehide(callback) {
  window.addEventListener('pagehide', callback, true)
}
export default { reportingMethod }
